const db = require('../db/index');
const _ = require('lodash')
    //封装报表1的函数
function reportOne(cb) {
    var sql = "select * from sp_report_1";
    db.query(sql, (err, result) => {
        if (err) return cb(err);
        // console.log(result);
        var areakeyResult = {}
        var araekeys = _.union(_.map(result, "rp1_area"));
        // console.log(araekeys);
        var datekeys = _.union(_.map(result, function(record) {
                // console.log(record);
                // console.log(record.rp1_date.getFullYear());
                str = record.rp1_date.getFullYear() + "-" + (record.rp1_date.getMonth() + 1) + "-" + record.rp1_date.getDate()
                return str
            }))
            // console.log(datekeys);
        for (idx in result) {
            var record = result[idx];
            var datekey = record.rp1_date.getFullYear() + "-" + (record.rp1_date.getMonth() + 1) + "-" + record.rp1_date.getDate()
                // areakeyResult[record['rp1_area']]
                // console.log(record['rp1_area']);
            if (!areakeyResult[record['rp1_area']]) {
                areakeyResult[record['rp1_area']] = {}
            }
            areakeyResult[record['rp1_area']][datekey] = record
        }
        // console.log(areakeyResult);
        //格式输出
        var series = [];
        _(araekeys).forEach(function(araekey) {
            // console.log(araekey);
            var data = [];
            _(datekeys).forEach(function(datekey) {
                    if (areakeyResult[araekey][datekey]) {
                        data.push(areakeyResult[araekey][datekey]["rp1_user_count"]);
                    } else {
                        data.push(0)
                    }
                })
                // console.log(data);
            series.push({
                name: araekey,
                type: 'line',
                areaStyle: {},
                stack: '总量',
                data: data
            })
        })
        var data = {
            title: {
                text: '用户来源'
            },
            legend: {
                data: araekeys
            },
            xAxis: [{
                data: datekeys
            }],
            yAxis: [
                { type: 'value' }
            ],
            series: series
        }
        cb(null, data)

    })
}

function reportTWO(cb) {

}


exports.getreports = (req, res) => {
    // res.ResData("获取数据报表")
    var typeid = req.params.typeid;
    // console.log(typeid);
    //参数验证
    //参数不能为空
    // if (!typeid) return res.ResData("报表类型不能为空");
    //参数必须是数字
    //业务逻辑

    reportOne((err, data) => {
            if (err) return res.ResData(err);
            res.ResData("获取报表数据成功", 200, data)
        })
        // console.log(parseInt(typeid));
        // switch (typeid) {
        //     case '1':
        //         reportOne((err, data) => {
        //             if (err) return res.ResData(err);
        //             return res.ResData("获取报表数据成功", 200, data)
        //         })
        //         break;
        //     case '2':
        //         reporttwo((err, data) => {
        //             if (err) return res.ResData(err);
        //             return res.ResData("获取报表数据成功", 200, data)
        //         })
        //         break;
        //     default:
        //         return res.ResData("类型出错")
        //         break;
        // }

}